package com.rub.android.widget

import com.rub.android.service.api.IGithubService
import com.rub.android.service.api.ITagsService
import com.rub.android.service.api.IUserService
import com.rub.android.utils.RetrofitHelper
import com.rub.android.utils.await

class RetrofitServiceApi  constructor(private val retrofit: RetrofitHelper) {


    /**
     * 查询标签
     */
    fun listTags() =
        retrofit.createApi(ITagsService::class.java, RetrofitHelper.BASE_URL).searchTags()

    /**
     * 查询标签
     */
    suspend fun listTagsAPI() =
        retrofit.createApi(ITagsService::class.java, RetrofitHelper.BASE_URL).listTags().await()


    /**
     * 用户登录
     */
    suspend fun login(userName: String, password: String) =
        retrofit.createApi(IUserService::class.java, RetrofitHelper.WAN_URL)
            .login(userName, password).await()

    /**
     * Github查询
     */
    suspend fun search(perPage: Int, page: Int,keyWord:String) =
        retrofit.createApi(IGithubService::class.java, RetrofitHelper.GITHUB_URL)
            .search(perPage, page,keyWord).await()


    companion object {

        private var serviceApi: RetrofitServiceApi? = null
        fun getInstance(): RetrofitServiceApi {
            if (serviceApi == null) {
                synchronized(RetrofitServiceApi::class.java) {
                    if (serviceApi == null) {
                        serviceApi = RetrofitServiceApi(RetrofitHelper())
                    }
                }
            }
            return serviceApi!!
        }
    }
}